package com.ruoyi.commentUpvote.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.comment.domain.Comment;
import com.ruoyi.comment.mapper.CommentMapper;
import org.aspectj.weaver.SourceContextImpl;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.commentUpvote.mapper.CommentUpvoteMapper;
import com.ruoyi.commentUpvote.domain.CommentUpvote;
import com.ruoyi.commentUpvote.service.ICommentUpvoteService;

import javax.annotation.Resource;

/**
 * 评论点赞Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-10
 */
@Service
public class CommentUpvoteServiceImpl implements ICommentUpvoteService 
{
    @Resource
    private CommentUpvoteMapper commentUpvoteMapper;

    /**
     * 查询评论点赞
     * 
     * @param id 评论点赞主键
     * @return 评论点赞
     */
    @Override
    public CommentUpvote selectCommentUpvoteById(Integer id)
    {
        return commentUpvoteMapper.selectCommentUpvoteById(id);
    }

    /**
     * 查询评论点赞列表
     * 
     * @param commentUpvote 评论点赞
     * @return 评论点赞
     */
    @Override
    public List<CommentUpvote> selectCommentUpvoteList(CommentUpvote commentUpvote)
    {
        return commentUpvoteMapper.selectCommentUpvoteList(commentUpvote);
    }

    /**
     * 新增评论点赞
     * 
     * @param commentUpvote 评论点赞
     * @return 结果
     */
    @Override
    public int insertCommentUpvote(CommentUpvote commentUpvote)
    {
        return commentUpvoteMapper.insertCommentUpvote(commentUpvote);
    }

    /**
     * 修改评论点赞
     * 
     * @param commentUpvote 评论点赞
     * @return 结果
     */
    @Override
    public int updateCommentUpvote(CommentUpvote commentUpvote)
    {

        return commentUpvoteMapper.updateCommentUpvote(commentUpvote);
    }

    @Resource
    private CommentMapper commentMapper;
//    wx修改评论点赞
    public Integer updateCommentUpvote(List<CommentUpvote> commentUpvoteList){
        for (CommentUpvote commentUpvote : commentUpvoteList) {
            if (commentUpvote.getId() != null) {
                Comment comment = commentMapper.selectCommentById(commentUpvote.getCommentId());
                CommentUpvote commentUpvote1 = commentUpvoteMapper.selectCommentUpvoteById(commentUpvote.getId());
                if (commentUpvote.getIsDeleted() != commentUpvote1.getIsDeleted()) {
                    if (commentUpvote.getIsDeleted() == 0) {
                        comment.setUpvoteNum(comment.getUpvoteNum() + 1);
                        commentMapper.updateComment(comment);
                    }else {
                        comment.setUpvoteNum(comment.getUpvoteNum() - 1);
                        commentMapper.updateComment(comment);
                    }
                }
                commentUpvoteMapper.updateCommentUpvote(commentUpvote);
            } else {
                commentUpvoteMapper.insertCommentUpvote(commentUpvote);
                Integer commentId = commentUpvote.getCommentId();
                Comment comment = commentMapper.selectCommentById(commentId);
                comment.setUpvoteNum(comment.getUpvoteNum() + 1);
                commentMapper.updateComment(comment);
            }
        }
        return 1;
    }

    /**
     * 批量删除评论点赞
     * 
     * @param ids 需要删除的评论点赞主键
     * @return 结果
     */
    @Override
    public int deleteCommentUpvoteByIds(Integer[] ids)
    {
        return commentUpvoteMapper.deleteCommentUpvoteByIds(ids);
    }

    /**
     * 删除评论点赞信息
     * 
     * @param id 评论点赞主键
     * @return 结果
     */
    @Override
    public int deleteCommentUpvoteById(Integer id)
    {
        return commentUpvoteMapper.deleteCommentUpvoteById(id);
    }
}
